//获取应用实例
const app = getApp()
import * as jobUtil from "../../utils/jobUtil"

Component({
  options: {
    addGlobalClass: true,
  },

  data: {
    noMore: false,
    noMoreData: false,
    jobList: [],
    pageNum:1,
    pageSize:20
  },

  lifetimes: {
    attached(){
      this.getJobList();
    },
  },


  methods: {

    
    getJobList(requestData,isLoad) {

      app.$loadingUtil.showLoading('加载中');

      let that = this;

      if (!isLoad) {
        this.data.pageNum = 1
      } else {
        this.data.pageNum += 1;
      }

      let wantWorkArea = '';
      if (requestData) {
        requestData.offset = (this.data.pageNum - 1) * this.data.pageSize;
        requestData.limit = this.data.pageSize;
        wantWorkArea = requestData.wantWorkArea
      }

      app.$http.getJobList(requestData).then(res => {
        that.setListData(res, wantWorkArea, isLoad);
      });
    },



    //搜索
    search(requestData, isLoad) {

      app.$loadingUtil.showLoading('加载中');

      let that = this;

      if (!isLoad) {
        this.data.pageNum = 1
      } else {
        this.data.pageNum += 1;
      }

      let wantWorkArea = '';
      if (requestData) {
        requestData.offset = (this.data.pageNum - 1) * this.data.pageSize;
        requestData.limit = this.data.pageSize;
        wantWorkArea = requestData.wantWorkArea
      }
      app.$http.searchJobList(requestData).then(res => {
        that.setListData(res, wantWorkArea, isLoad);
      });
    },


    setListData(res, wantWorkArea, isLoad) {
      wx.stopPullDownRefresh();
      app.$loadingUtil.hideLoading();

      if (!isLoad) {
        this.data.jobList = [];
      }

      if (res.length < this.data.pageSize) {
        this.data.noMoreData = true;
      } else {
        this.data.noMoreData = false;
      }

      for (let i = 0; i < res.length; i++) {
        let item = res[i];
        let job = {
          id: item.id,
          title: item.title,
          jobType: item.jobType,
          jobTime: jobUtil.getJobShift(item),
          storeName: item.storeName,
          price: jobUtil.getPrice(item) + jobUtil.commissionUnit(item.jobPayUnit),
          settlementType: jobUtil.settlementType(item),
          require: jobUtil.experienceRequire(item.experienceRequire) + "工作经验" + " | " + jobUtil.getAge(item) + " | " + jobUtil.genderRequire(item.genderRequire) + " | " + jobUtil.educationRequire(item.educationRequire),
          welfares: item.welfares,
          distance: wantWorkArea && "距离" + wantWorkArea + jobUtil.getDistance(item.distance),
        };
        this.data.jobList.push(job);
      }
      this.setData({
        jobList: this.data.jobList,
        noMoreData: this.data.noMoreData,
        noMore: this.data.jobList.length < 1
      })
    },

    goJobDetail(e) {

      const {
        id,
        jobtype
      } = e.currentTarget.dataset;
      // const {postID} = e.
      // console.log(id)
      // console.log(jobtype)
      let passMap = {
        "id": id,
        "jobType": jobtype
      }
      getApp().$routes.goTo(getApp().$routes.postDetail, passMap);
    }
  }
})